﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_MBouquet : System.Web.UI.Page
{
    BouquetBUS bouBUS = new BouquetBUS();
    DataTable tbBou = new DataTable();
    DataView viewBOU;
    CatalogBUS caBUS = new CatalogBUS();
    DataTable tbCatalog = new DataTable();
    List<Bouquet> lstBouquet;
    DataTable tbdrlCatalog = new DataTable();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
            LoadView2();
        }
        LoadData();
    }
    public void LoadData()
    {
        lstBouquet = new List<Bouquet>();
        tbBou = bouBUS.returnAllBouquet();
        viewBOU = new DataView(tbBou);
        viewBOU.RowFilter = "B_Status=1";
        for (int i = 0; i < viewBOU.Count; i++)
        {
            tbCatalog = caBUS.returnCatalogByID(Int32.Parse(viewBOU[i]["C_ID"].ToString()));
            if (tbCatalog.Rows.Count > 0)
            {
                Bouquet item = new Bouquet(viewBOU[i]["B_ID"].ToString(), viewBOU[i]["B_Name"].ToString(), float.Parse(viewBOU[i]["B_Price"].ToString()), viewBOU[i]["B_Details"].ToString(), viewBOU[i]["B_Image"].ToString(), tbCatalog.Rows[0]["C_Name"].ToString(), Int32.Parse(viewBOU[i]["B_Home"].ToString()));
                lstBouquet.Add(item);
            }
        }

        grvBou.DataSource = lstBouquet;
        grvBou.DataBind();

    }
    public void LoadView2()
    {
        tbdrlCatalog = caBUS.returnAllCatalog();

        drlCatalog.DataSource = tbdrlCatalog;
        drlCatalog.DataTextField = "C_Name";
        drlCatalog.DataValueField = "C_ID";
        drlCatalog.DataBind();
    }
    protected void grvBou_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvBou.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void grvBou_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DelItem"))
        {
            bool check = bouBUS.updateStatusBouquet(e.CommandArgument.ToString());
            if (check)
            {
                LoadData();
            }
        }
        if (e.CommandName.Equals("UpdateItem"))
        {
            MultiView1.ActiveViewIndex = 1;

            foreach (Bouquet bou in lstBouquet)
            {
                if (bou.Id.Equals(e.CommandArgument.ToString()))
                {
                    txtID.Text = bou.Id;
                    txtName.Text = bou.Name;
                    txtDetails.Text = bou.Details;
                    txtPrice.Text = bou.Price.ToString();
                    imImage.ImageUrl = bou.Image;
                    lblUrl.Text = bou.Image;

                    for (int i = 0; i < drlCatalog.Items.Count; i++)
                    {
                        if (drlCatalog.Items[i].Text.Equals(bou.Cid))
                        {
                            drlCatalog.SelectedIndex = i;
                        }
                    }
                    if (bou.Home > 0)
                    {
                        cbxHome.Checked = true;
                    }
                    else
                    {
                        cbxHome.Checked = false;
                    }
                }
            }
        }
    }
   
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string id = "";
        string name = "";
        float price = 0;
        string details = "";
        string image = "";
        int cid = 0;
        int home = 0;
        string path = Server.MapPath("..");
        if (txtName.Text.Equals("") || txtPrice.Text.Equals("") || txtDetails.Text.Equals(""))
        {
            lblError.Text = "- You must enter enough information";
        }
        else
        {
            id = txtID.Text;
            name = txtName.Text;
            price = float.Parse(txtPrice.Text);
            details = txtDetails.Text;
            if (FileUpload1.HasFile)
            {
                try
                {
                    image = "../images/" + FileUpload1.FileName;
                    FileUpload1.SaveAs(path + "\\images\\" + FileUpload1.FileName);
                }
                catch (Exception ex)
                {
                    lblError.Text = "- Upload file error";
                }

            }
            else
            {
                image = lblUrl.Text;
            }
            cid = Int32.Parse(drlCatalog.SelectedValue);
            if (cbxHome.Checked)
            {
                home = 1;
            }
            else
            {
                home = 0;
            }
            bool check = bouBUS.updateBouquet(id, name, price, details, image, cid, home);
            if (check)
            {
                lblError.Text = "- Update Bouquet Success";
            }
            else
            {
                lblError.Text = "- Update Bouquet Error";
            }
        }
    }
    protected void lbnSearch_Click(object sender, EventArgs e)
    {
        int count = 0;
        List<Bouquet> temp = new List<Bouquet>();
        if (txtFindValue.Value.Length > 0)
        {
            foreach (Bouquet bou in lstBouquet)
            {
                if (bou.Id.Equals(txtFindValue.Value))
                {
                    temp.Add(bou);
                }
                else
                {
                    count++;
                }
            }
            if (count == lstBouquet.Count)
            {
                grvBou.DataSource = null;
                grvBou.DataBind();
            }
            else
            {
                grvBou.DataSource = temp;
                grvBou.DataBind();
            }
        }
    }
}